﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Air_condition.DO
{
    [Serializable]
    public class InternalLoad
    {
        // Attributes of internal load object (private)
        private string sname;
        private int igender = -1;
        private double dblatentHeat;
        private double dbsensibleHeat;
        private double dbdensityPeople;
        private double dbpowerToolsSensibleHeat;
        private double dbairMountForOnePeople;
        private int ispaceConditioning = -1;

        // List equipment
        public ObservableCollection<Equip> lequip = new ObservableCollection<Equip>();        

        // Public access to attributes
        public string sName
        {
            get { return sname; }
            set { sname = value; }
        }

        public double dbAirMountForOnePeople
        {
            get { return dbairMountForOnePeople; }
            set { dbairMountForOnePeople = value; }
        }

        public double dbDensityPeople
        {
            get { return dbdensityPeople; }
            set { dbdensityPeople = value; }
        }

        public double dbSensibleHeat
        {
            get { return dbsensibleHeat; }
            set { dbsensibleHeat = value; }
        }

        public double dbLatentHeat
        {
            get { return dblatentHeat; }
            set { dblatentHeat = value; }
        }

        public int iGender
        {
            get { return igender; }
            set { igender = value; }
        }

        public int iSpaceConditioning
        {
            get { return ispaceConditioning; }
            set { ispaceConditioning = value; }
        }

        //  Nhiệt hiện do người tỏa ra Q31
        /// <summary>
        /// Method to calculate sensible heat from people (Q31)
        /// </summary>
        /// <returns>sensible heat from people (double number)</returns>
        public double CalculatePeopleSensibletHeat(double dbarea)
        {
            if (dbdensityPeople == 0)
            {
                return 0;
            }
            return (dbarea / dbdensityPeople) * dbsensibleHeat;
        }

        //  Nhiệt hiện do đèn chiếu sáng Q32
        /// <summary>
        /// Method to calculate sensible heat from light (Q32)
        /// </summary>
        /// <param name="dbarea">area of room</param>
        /// <returns>sensible heat from light (double number)</returns>
        public double CalculateLightSensibleHeat(double dbarea)
        {
            return dbarea * 12;
        }

        //  Nhiệt hiện tỏa ra từ dụng cụ điện Q33
        /// <summary>
        /// Method calculate sensible heat from equipments
        /// </summary>
        /// <returns>sensible heat from equipments (double number)</returns>
        public double CalculatePowerToolsSensibleHeat()
        {
            foreach (Equip e in lequip)
            {
                dbpowerToolsSensibleHeat += e.dbEnergy;
            }
            return dbpowerToolsSensibleHeat;
        }

        //  Nhiệt ẩn do người tỏa ra Qaf
        /// <summary>
        /// Method to calculate latent heat from people (Qaf)
        /// </summary>
        /// <returns>latent heat from people (double member)</returns>
        public double CalculatePeopleLatentHeat(double dbarea)
        {
            if (dbdensityPeople == 0)
            {
                return 0;
            }
            return (dbarea / dbdensityPeople) * dblatentHeat;
        }

        /// <summary>
        /// method to copy Room object to a new object
        /// </summary>
        /// <returns>a new object is Room object type</returns>
        public object Clone()
        {
            object clone;
            try
            {
                using (MemoryStream stream = new MemoryStream())
                {

                    BinaryFormatter formatter = new BinaryFormatter();

                    // Serialize this object

                    formatter.Serialize(stream, this);

                    stream.Position = 0;

                    // Deserialize to another object

                    clone = formatter.Deserialize(stream);

                }
                return clone;
            }
            catch (Exception e)
            {
                return e;
            }
        }
    }
}
